package kara;

import java.io.*;

public class A99 {
static int n;
static int m;
static int[][] map;
static int[] dir1={1,0,-1,0};
static int[] dir2={0,1,0,-1};
 static   BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
 static   BufferedWriter log=new BufferedWriter(new OutputStreamWriter(System.out));
    public static void main(String[] args) throws IOException {
        int res=0;
        String[] s = read.readLine().split(" ");
         n = Integer.parseInt(s[0]);
         m=Integer.parseInt(s[1]);
         map=new int[n][m];
         for(int i=0;i<n;i++){
             String[] h = read.readLine().split(" ");
             for(int j=0;j<m;j++){
                 map[i][j]=Integer.parseInt(h[j]);
             }
         }
//深搜
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(map[i][j]==1){
//标记为海洋
                    //搜索
                    map[i][j]=0;
               dfs(i,j);
res++;
                }
            }
        }
        log.write(Integer.toString(res));
        log.flush();
        read.close();
        log.close();
    }
    static void dfs(int x,int y){
//遍历四周，对合法节点dfs
        for(int i=0;i<4;i++){
            int a=x+dir1[i];
            int b=y+dir2[i];
            if(a>=0&&a<n&&b>=0&&b<m&&map[a][b]==1){
                map[a][b]=0;//标记为海洋
                dfs(a,b);
            }
        }
    }
}
